package com.qf.file.service.impl;

import com.qf.base.util.AsyncBatchCallUtil;
import com.qf.base.util.SyncBatchCallUtil;
import com.qf.file.dao.GifWorkDao;
import com.qf.file.model.GifWork;
import com.qf.file.model.GifWorkExample;
import com.qf.file.service.GifService;
import com.qf.file.util.AliyunOSSUtil;
import com.qf.file.util.GifUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

import static com.qf.file.controller.FileController.FILE_PATH;

/**
 * @author 汤奇
 * @version V1.0
 * @Project BookPro
 * @Package com.qf.file.service.impl
 * @Description:
 * @Date 2022/8/5 21:22
 */
@Service
public class GifServiceImpl implements GifService {
    @Resource
    private GifWorkDao gifWorkDao;

    @Override
    public void createGif(String fileName) {
        GifWork gifWorkInsert = new GifWork();
        gifWorkInsert.setFileName(fileName);
        gifWorkInsert.setState(0);
        gifWorkDao.insertSelective(gifWorkInsert);

        AsyncBatchCallUtil.execute( () -> {
            GifUtil.buildGifAndJpg(FILE_PATH+ File.separator+fileName+".mp4",5,20,5,10);
            File videoFile = new File(FILE_PATH + File.separator + fileName + ".mp4");
            File gifFile = new File(FILE_PATH + File.separator + fileName + ".gif");
            File jpgFile = new File(FILE_PATH + File.separator + fileName + ".jpg");

            String[] url = {null,null,null};
            SyncBatchCallUtil.batch(new SyncBatchCallUtil.Task() {
                @Override
                public void exe() {
                    url[0] = AliyunOSSUtil.uploadImage(fileName + ".mp4", videoFile);
                }
            }, new SyncBatchCallUtil.Task() {
                @Override
                public void exe() {
                    url[1] = AliyunOSSUtil.uploadImage(fileName + ".gif", gifFile);
                }
            }, new SyncBatchCallUtil.Task() {
                @Override
                public void exe() {
                    url[2] = AliyunOSSUtil.uploadImage(fileName+".jpg",jpgFile);
                }
            });

            GifWork gifWork = new GifWork();
            gifWork.setVideoUrl(url[0]);
            gifWork.setGifUrl(url[1]);
            gifWork.setJpgUrl(url[2]);
            gifWork.setState(1);

            GifWorkExample example = new GifWorkExample();
            GifWorkExample.Criteria criteria = example.createCriteria();
            criteria.andFileNameEqualTo(fileName);
            gifWorkDao.updateByExampleSelective(gifWork,example);
        });

    }

    @Override
    public GifWork selectGifWork(String fileName) {
        GifWorkExample example = new GifWorkExample();
        GifWorkExample.Criteria criteria = example.createCriteria();
        criteria.andFileNameEqualTo(fileName);
        List<GifWork> gifWorks = gifWorkDao.selectByExample(example);
        if (gifWorks != null && gifWorks.size() > 0){
            return gifWorks.get(0);
        }
        return null;
    }
}
